package cn.yunhe.action.temp;

import cn.yunhe.util.BaseAction;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUploadAction1 extends BaseAction {
    private String title;

    private File upload;
    private String uploadContentType;
    private String uploadFileName;

    private String savePath;

    @Override
    public String execute() throws Exception {
        String upload2SavePath=getSavePath()+"\\"+getUploadFileName();
        doUpload(upload,upload2SavePath);
        return SUCCESS;
    }

    public void doUpload(File file,String finalSavePath) throws IOException {
        FileInputStream fis=new FileInputStream(file);

        byte[] buffer=new byte[12];
        int length=0;

        FileOutputStream fos=new FileOutputStream(finalSavePath);

        length=fis.read(buffer);
        while (length!=-1){
            fos.write(buffer);
            //length=fis.read(buffer);
        }

        fis.close();
        fos.close();

    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
